<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新增栏目</title>
    <link rel="stylesheet" href="__ADMIN__/frame/layui/css/layui.css">
    <link rel="stylesheet" href="__ADMIN__/frame/static/css/style.css">
    <link rel="icon" href="__ADMIN__/frame/static/image/code.png">
</head>
<body class="body">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>新增栏目</legend>
</fieldset>

<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;font-weight: bold;">*</span>所在栏目</label>
        <div class="layui-input-block">
<select name="pid"  lay-filter="aihao">
        <option value="">请选择所在栏目</option>
        <option value="0">****作为一级栏目</option>
              {volist name="data" id="v"}
                 <option value="{$v.id}">{$v.Lname}</option>
             {/volist}     
        </select>
        </div>
    </div>  
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;font-weight: bold;">*</span>栏目名称</label>
        <div class="layui-input-block">
            <input type="text" name="Lname" lay-verify="required" autocomplete="off" placeholder="请输入用户名" class="layui-input" maxlength="20">
        </div>
    </div>
     <div class="layui-form-item">
         <label class="layui-form-label">栏目图片</label>
              <div class="layui-input-block">
                    <button type="button" class="layui-btn" id="logo">选择图片</button>
                    <div class="layui-upload-list">
                    <input type="hidden" name="img" id="img" autocomplete="off" class="layui-input">
                    <img class="layui-upload-img" src="" width="120" height="80" style="display: none;" id="logo-effect">
            </div>
            </div>
        </div>
    </div>
     <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;font-weight: bold;">*</span>跳转模块</label>
        <div class="layui-input-block">
            <input type="text" name="module" lay-verify="required" autocomplete="off" placeholder="请输入用户名" class="layui-input" maxlength="30">
        </div>
    </div>
     <div class="layui-form-item">
        <label class="layui-form-label">栏目描述</label>
        <div class="layui-input-block">
            <input type="text" name="desc"  autocomplete="off" placeholder="请输入用户名" class="layui-input" maxlength="30">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;font-weight: bold;">*</span>栏目位置</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="top" title="顶部" checked>
             <input type="radio" name="type" value="bottom" title="底部" >
      </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" id="submit" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="__ADMIN__/frame/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate','upload','jquery'], function(){
        var form = layui.form,
                layer = layui.layer,
                layedit = layui.layedit,
                $ = layui.jquery,
                upload = layui.upload,
                laydate = layui.laydate; 
        
       //监听提交
        form.on('submit(demo1)', function(data){
            var index = parent.layer.getFrameIndex(window.name); 
            $.ajax({
                type: 'post',
                url: "{:url('Category/addCategory')}",
                data: data.field,
                dataType: 'json',
                success: function(data){
                    console.log(data)
                    if(data.status == 0){
                        layer.msg(data.msg, {
                          icon: 1,
                          time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                          parent.layer.close(index);
                        });
                    } else {
                        layer.msg(data.msg,{icon: 5,time:1000});
                    }
                },
                error:function(data) {
                    layer.msg('提交失败!',{icon: 5,time:1000});
                },
            });
            return false;
        });


        //上传栏目图片
        upload.render({
            elem: '#logo'
            ,url: "{:url('Uploads/upload')}"
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo-effect').attr('src', result);
                    $("#logo-effect").css('display','block'); 
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.status == 0){
                    $('#img').val(res.data);
                    layer.msg(res.msg, {
                      icon: 1,
                      time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    });
                } else {
                    layer.msg(res.msg,{icon: 5,time:1000});
                }
                //上传成功
            }
        });


        //验证
        form.verify({
          Lname: function(value, item){
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
              return '用户名不能有特殊字符';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
              return '用户名首尾不能出现下划线\'_\'';
            }
            if(/^\d+\d+\d$/.test(value)){
              return '用户名不能全为数字';
            }
          },
          pass: function(value,item){
            var reg = /^[\S]{6,16}$/;
            if(!reg.test(value)){
              return '密码必须6到16位，且不能出现空格';
            }
            if(/^\d+\d+\d$/.test(value)){
              return '密码不能全为数字';
            }
          }
        }); 

    });
</script>
</body>
</html>